/**
 * Filename: AlbumMemberAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/8 12:11
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNTransAction;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;


public class TransactionsAdapter extends RecyclerView.Adapter<TransactionsAdapter.ViewHolder> {
    private Context mContext;
    private List<YNTransAction> mDatas;
    private HashMap<Integer, String> datemap;

    public TransactionsAdapter(Context mContext, List<YNTransAction> datas) {
        this.mContext = mContext;
        this.mDatas = datas;
        datemap = new HashMap<>();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout year_mouth_layout;
        TextView year_mouth;
        TextView transactionType;
        TextView content;
        TextView price;
        TextView time;

        public ViewHolder(View itemView) {
            super(itemView);
            year_mouth_layout = (LinearLayout) itemView
                    .findViewById(R.id.year_mouth_layout);
            year_mouth = (TextView) itemView
                    .findViewById(R.id.year_mouth);
            transactionType = (TextView) itemView
                    .findViewById(R.id.transactiontype);
            content = (TextView) itemView
                    .findViewById(R.id.content);
            price = (TextView) itemView
                    .findViewById(R.id.price);
            time = (TextView) itemView.findViewById(R.id.time);
        }

        public void bindData(final YNTransAction listItems, int position) {
            if (listItems != null) {
                transactionType.setText(listItems.getTransactionType());
                if (listItems.getTransactionType().equals("充值")) {
                    content.setText("充值成功");
                } else if (listItems.getTransactionType().equals("收红包")) {
                    content.setText("已存入钱包");
                } else if (listItems.getTransactionType().equals("发红包")) {
                    content.setText("支付成功");
                } else if (listItems.getTransactionType().equals("提现")) {
                    content.setText("提现成功");
                } else if (listItems.getTransactionType().equals("退款")) {
                    content.setText("退款成功");
                } else {
                    content.setText("成功");
                }
                if (listItems.getTransactionMoney().contains("-")) {
                    price.setTextColor(Color.parseColor("#333333"));
                    price.setText(listItems.getTransactionMoney() + "元");
                } else {
                    price.setTextColor(Color.parseColor("#1eb663"));
                    price.setText("+" + listItems.getTransactionMoney() + "元");
                }
                try {
                    Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).parse(listItems.getTransactionTime());
                    String InYear = DateFormat.format("yyyy", date).toString();
                    String InMouth = DateFormat.format("MM", date).toString();
                    String Indd = DateFormat.format("dd", date).toString();
                    String Inhhmmss = DateFormat.format("HH:mm:ss", date).toString();
                    String dateItem = InYear + "-" + InMouth;
                    String dateItem2 = InYear + "年" + InMouth + "月";
                    datemap.put(position, dateItem);
                    time.setText(dateItem + "/" + Inhhmmss);
                    if (position == 0) {
                        year_mouth.setText(dateItem2);
                        year_mouth_layout.setVisibility(View.VISIBLE);
                    } else {
                        if (TextUtils.equals(dateItem, datemap.get(position - 1))) {
                            year_mouth_layout.setVisibility(View.GONE);
                        } else {
                            year_mouth.setText(dateItem2);
                            year_mouth_layout.setVisibility(View.VISIBLE);
                        }
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private YNTransAction getItem(int position) {
        return mDatas.get(position);
    }


    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_transaction, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        YNTransAction listItems = getItem(position);
        holder.bindData(listItems, position);
        holder.itemView.setTag(listItems);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mDatas.size() > 0 ? mDatas.size() : 0;
    }

    public void addItemsFromTop(List<YNTransAction> items) {
        if (items == null || items.size() == 0) {
            this.mDatas.clear();
            this.notifyItemRangeInserted(0, 0);
            this.notifyDataSetChanged();
            return;
        }
        this.mDatas.addAll(0, items);
        this.notifyItemRangeInserted(0, items.size());
        this.notifyDataSetChanged();
    }

    public void addItemsFromTopOne(YNTransAction items) {
        if (items == null)
            return;
        this.mDatas.add(0, items);
        this.notifyItemRangeInserted(0, 1);
        this.notifyDataSetChanged();
    }

    public void removeOne(int ids) {
        for (int i = 0; i < mDatas.size(); i++) {
            if (ids == mDatas.get(i).getTransactionID()) {
                this.mDatas.remove(i);
                this.notifyItemRemoved(i);
                break;
            }
        }
    }

    public void addItemsFromBottom(List<YNTransAction> items) {
        if (items == null)
            return;
        this.mDatas.addAll(mDatas.size(), items);
        this.notifyItemRangeInserted(mDatas.size(), items.size());
    }


    public int getIdByDirection(boolean isTop) {
        if (isTop) {
            if (mDatas.size() <= 0) {
                return 0;
            }
            return mDatas.get(0).getTransactionID();
        } else {
            if (mDatas.size() <= 0) {
                return 0;
            }
            return mDatas.get(mDatas.size() - 1).getTransactionID();
        }
    }

}
